<template>
  <div>
    <el-card>
      <el-row style="margin-bottom: 10px">
        <el-col span="2" style="margin-right: 10px">
          <el-input placeholder="反馈用户ID" v-model="selectUserId"></el-input>
        </el-col>
        <el-col :span="3" style="margin-right: 10px">
          <el-select v-model="selectType" placeholder="反馈类型" value="">
            <el-option label="" value=""></el-option>
            <el-option :label="item.label" :value="item.value" v-for="item in typeList"></el-option>
          </el-select>
        </el-col>
        <el-col :span="3" style="margin-right: 10px">
          <el-select v-model="selectFunction" placeholder="功能模块" value="">
            <el-option label="" value=""></el-option>
            <el-option :label="item.functionName" :value="item.id" v-for="item in functionList"></el-option>
          </el-select>
        </el-col>
        <el-col span="auto" style="margin-right: 10px">
          <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        </el-col>
      </el-row>
      <el-table :data="tableData" border>
        <el-table-column label="ID" prop="id" width="60" header-align="center" align="center" sortable>
          <template slot-scope="scope">
            <span v-html="scope.row.id"></span>
          </template>
        </el-table-column>
        <el-table-column label="反馈用户ID" prop="userId" width="120" header-align="center" align="center" sortable>
          <template slot-scope="scope">
            <el-popover placement="right" trigger="click">
              <UserInfoCard :userId="scope.row.userId" :key="scope.row.userId"></UserInfoCard>
              <el-button size="mini" plain type="primary" slot="reference" @click="getUserId(scope.row.userId)">{{
                scope.row.userId }}</el-button>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="功能模块" prop="functionName" width="100" header-align="center"
          align="center"></el-table-column>
        <el-table-column label="反馈类型" prop="type" width="100" header-align="center" align="center">
          <template slot-scope="scope">
            <span>{{ typeList[scope.row.type].label }}</span>
          </template>
        </el-table-column>
        <el-table-column label="标题" prop="title" width="200" header-align="center" align="center">
          <template slot-scope="scope">
            <span v-html="scope.row.title"></span>
          </template>
        </el-table-column>
        <el-table-column label="内容" prop="content" min-width="230" header-align="center">
          <template slot-scope="scope">
            <span v-html="scope.row.content"></span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="gmtCreate" min-width="120" header-align="center" sortable></el-table-column>
        <el-table-column label="操作" align="center" header-align="center">
          <template slot-scope="scope">
            <el-button size="mini" @click="deleteDialogOpen(scope.row)" type="danger">删除</el-button>
            <el-dialog title="确认删除" :visible.sync="deleteDialog" width="35%">
              <el-form label-width="100px">
                <p>真的确定删除吗！！！</p>
              </el-form>
              <span slot="footer" class="dialog-footer">
                <el-button @click="deleteDialog = false">取 消</el-button>
                <el-button type="primary" @click="handleDelete(id)">确 定</el-button>
              </span>
            </el-dialog>
          </template>
        </el-table-column>
      </el-table>

      <!-- 底部分页 -->
      <div style="text-align: center">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page.sync="currentPage" :page-sizes="[10, 15, 20]" :page-size="pageSize" :total="totalSize" background
          layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { getAllFunction } from '@/api/system/function'
import { getFeedbackList } from '@/api/system/feedback'
import { deleteFeedback } from '@/api/system/feedback'
import UserInfoCard from '@/components/UserInfoCard.vue'

export default {
  components: { UserInfoCard },
  provide() {
    return {
      getDataList: this.getDataList,
    }
  },
  data() {
    const validate = (rule, value, callback) => {
      if (value.length > 0) {
        callback()
      } else {
        callback(new Error('不能为空'))
      }
    }
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      totalSize: 0,
      id: '',
      userDialogue: false,
      deleteDialog: false,
      rule: {
        functionName: [{ required: true, trigger: 'blur', validator: validate }],
        functionCode: [{ required: true, trigger: 'blur', validator: validate }],
        description: [{ required: true, trigger: 'blur', validator: validate }]
      },
      selectFunction: '',
      selectUserId: '',
      selectType: '',
      functionList: [],
      typeList: [{
        label: '困惑与建议',
        value: 0
      }, {
        label: 'Bug缺陷',
        value: 1
      }, {
        label: '申诉',
        value: 2
      }]
    }
  },
  methods: {
    deleteDialogOpen(row) {
      this.deleteDialog = true
      this.id = row.id
      console.log(this.id)
    },
    handleDelete(id) {
      deleteFeedback([
        id
      ]).then(res => {
        if (res.data.code === 200) {
          this.deleteDialog = false
          this.$message.success({
            message: res.data.message,
            duration: 1000
          })
          this.getDataList()
        } else {
          // this.$message.error({
          //   message: res.data.message,
          //   duration: 1000
          // })
        }
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getDataList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getDataList()
    },
    getUserId(userId) {
      this.userDialogue = true
      this.userId = Number(userId)
    },
    getFunctionList() {
      getAllFunction().then(res => {
        if (res.data.code === 200) {
          this.functionList = res.data.functionInfoList
        } else {
          // this.$message.error({
          //   message: res.data.message,
          //   duration: 1000
          // })
        }
      })
    },
    getDataList() {
      getFeedbackList({
        functionId: this.selectFunction,
        pageNo: this.currentPage,
        pageSize: this.pageSize,
        type: this.selectType,
        userId: this.selectUserId
      }).then(res => {
        if (res.data.code === 200) {
          this.tableData = res.data.pageResult.dataList
          this.totalSize = res.data.pageResult.totalCount
          this.getFunctionList()
        } else {
          // this.$message.error({
          //   message: res.data.message,
          //   duration: 1000
          // })
        }
      })
    },
    search() {
      getFeedbackList({
        functionId: this.selectFunction,
        pageNo: 1,
        pageSize: this.pageSize,
        type: this.selectType,
        userId: this.selectUserId
      }).then(res => {
        if (res.data.code === 200) {
          this.tableData = res.data.pageResult.dataList
          if (this.tableData === null || this.tableData === undefined) {
            this.tableData = []
          }
          this.totalSize = res.data.pageResult.totalCount
          this.getFunctionList()
        } else {
          // this.$message.error({
          //   message: res.data.message,
          //   duration: 1000
          // })
        }
      })
    }
  },
  created() {
    this.getDataList()
  }
}
</script>

<style lang="scss" scoped>
.el-pagination {
  margin-top: 20px;
}
</style>
